package kr.co.happenstance;

import java.util.ArrayList;

import kr.co.happenstance.adapter.NoticeAdapter;
import kr.co.happenstance.lib.NoticeLib;
import kr.co.happenstance.lib.vo.LibResultVo;
import kr.co.happenstance.lib.vo.NoticeInfo;
import kr.co.happenstance.lib.vo.NoticeListResultVo;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class NoticeActivity extends BaseActivity implements View.OnClickListener, OnScrollListener, OnItemClickListener{
	protected final String TAG = "<<<NoticeActivity>>>";
	
	/**
	 * 현재 페이지
	 */
	private int currentPage;
	
	/**
	 * 총페이지
	 */
	private int totalPage;	
	
	/**
	 * ListView Adapter 객체
	 */
	private NoticeAdapter noticeAdapter; 
	
	/**
	 * Adapter ArrayList객체
	 */
	private ArrayList<NoticeInfo> noticeList;
	
	/**
	 * ListView 객체
	 */
	private ListView lvNoticeList;	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_notice);
		
		lvNoticeList = (ListView) this.findViewById(R.id.lv_notice_list);
		
		noticeList = new ArrayList<NoticeInfo>();
		noticeAdapter =  new NoticeAdapter(this, noticeList);
		lvNoticeList.setAdapter(noticeAdapter);
		lvNoticeList.setOnScrollListener(this);		
		lvNoticeList.setOnItemClickListener(this);
		
		currentPage=1;
		new NoticeTask().execute();
		
		addActivity(this);		
		
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
			case R.id.btn_pop_close:
				Log.d(TAG, "btn_pop_close onClick.....");
				finish();
				break;
		}		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Log.d(TAG, "onItemClick postion["+position+"]");
		Intent intent = new Intent(this, NoticeDetailActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intent.putExtra("seq", noticeList.get(position).seq);
		startActivity(intent);			
		
		
	}		
	
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		//현재 가장 처음에 보이는 셀번호와 보여지는 셀번호를 더한값이 전체의 숫자와 동일해지면 가장 아래로 스크롤 되었다고 가정합니다.		
		int count = totalItemCount - visibleItemCount;
		if(firstVisibleItem >= count && totalItemCount != 0){
			
			currentPage++;
			if(currentPage <= totalPage){
				Log.d(TAG, "Loading next items");
				new NoticeTask().execute();	
			}
		}	
	}

	@Override
	public void onScrollStateChanged(AbsListView arg0, int arg1) {
	}	
	
	
	private class NoticeTask extends AsyncTask<Void, Void, LibResultVo> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			showProgress();
		}	
		
		@Override
		protected LibResultVo doInBackground(Void... arg0) {
			LibResultVo vo = null;
			try{
				NoticeLib lib = new NoticeLib();
				vo = lib.list(currentPage);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			return vo;
		}
		
		@Override
		protected void onPostExecute(LibResultVo resultVo) {
			closeProgress();
			
			NoticeListResultVo vo = (NoticeListResultVo) resultVo;
			if(vo == null){
				Log.d(TAG, "NoticeListResultVo null......");
				new CustomAlertDialogBuilder(NoticeActivity.this, R.string.alert_title, R.string.alert_msg_exception).create();
			}else{
				totalPage = vo.getTotalPage();
				Log.d(TAG, "totalPage["+totalPage+"]");
				if(totalPage > 0){
					ArrayList<NoticeInfo> tmpNoticeList = vo.getNoticeList();
					for(NoticeInfo v: tmpNoticeList){
						noticeList.add(v);
					}
					noticeAdapter.notifyDataSetChanged();
				}
				
				Log.d(TAG, "list.size()["+noticeList.size()+"]");
			}
			
		}	
		
	}



	
}
